﻿using _08_Prism框架.Event;
using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _08_Prism框架.ViewModels;

public class ViewCViewModel : IDialogAware
{

    private IEventAggregator _eventAggregator;

    public ViewCViewModel(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        YesCommand = new DelegateCommand(() =>
        {
            OnDialogClosed();
        });
        CancleCommand = new DelegateCommand(() =>
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
        });
        // 向MessageEvent事件的订阅者发布消息
        eventAggregator.GetEvent<MessageEvent>().Publish("ViewCViewModel已创建");
    }

    public DelegateCommand YesCommand { get; set; }
    public DelegateCommand CancleCommand { get; set; }

    public string? Title { get; set; }

    public event Action<IDialogResult>? RequestClose;

    public bool CanCloseDialog()
    {
        return true;
    }

    public void OnDialogClosed()
    {
        var parameters = new DialogParameters();
        parameters.Add("Value", "对话框传递的参数");
        RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
    }

    public void OnDialogOpened(IDialogParameters parameters)
    {
        Title = parameters.GetValue<string>("Title");
    }
}
